<?php
include "../includes/verificasession.php";

session_start();
	
if($_POST){

	include ("../includes/config_conexao.php");
	
	$sqlBusca = "select s.id_Servico, c.desc_cliente, concat(date_format(s.data_prev_servico,'%d/%m/%Y'),' - ',TIME_FORMAT(s.hr_prev_exec_servico,'%H:%i')) data_prev_servico,
				        s.assunto_servico, s.vlr_servico, s.vlr_desc_servico, sa.desc_status, tp.desc_tipo_pagamento, u.desc_usuario
				from servico s
				inner join atendimento a on s.id_servico = a.id_servico
				inner join historico_atendimento ha on s.id_servico = a.id_servico
				inner join cliente c on c.id_cliente = s.id_cliente
				left join usuario u on s.id_usuario = u.id_usuario
				inner join status_atendimento sa on sa.id_status = ha.id_status 
				left join tipo_pagamento tp on tp.id_tipo_pagamento = s.id_tipo_pagamento
				where s.status_servico = 'A'
					and ha.id_historico = (select id_historico 
					                        from historico_atendimento
					                        where id_atendimento = a.id_Atendimento
					                        order by data_inc_historico desc
					                        limit 1) ";
	
	if(!empty($_POST["strCliente"])){
		$sqlBusca .= " and upper(c.desc_cliente) like upper('%".$_POST["strCliente"]."%') ";
	}
	if(!empty($_POST["idStatusPagamento"])){
		$sqlBusca .= " and ha.id_status = ".$_POST["idStatusPagamento"]." ";
	}
	if(!empty($_POST["idTipoPagamento"])){
		$sqlBusca .= " and s.id_tipo_pagamento = ".$_POST["idTipoPagamento"]." ";
	}
	if(!empty($_POST["idTecnico"])){
		$sqlBusca .= " and s.id_usuario = ".$_POST["idTecnico"]." ";
	}
	if($_POST["flPagamento"] == "1"){
		$sqlBusca .= " and s.vlr_pag_servico is not null and data_pag_servico is not null ";
	}

	$resposta = mysql_query($sqlBusca);
	
	$strResp = '<table id="TabelaRel" style="width:100%;">
							<thead>
								<tr style="background-color: #6CF; color:#000; font-style:italic;">
									<th>Cliente</th>
									<th>Data/Hora serviço</th>
									<th>Assunto</th>
									<th>Valor</th>
									<th>Valor desconto</th>
									<th>Status</th>
									<th>Forma de pagamento</th>
									<th>Técnico</th>
								</tr>
							</thead>
							<tbody style="text-align:center;">';

	if(mysql_num_rows($resposta) > 0){
		$cor = "#B7E8FF";
		while($ln = mysql_fetch_array($resposta)){
			$cor = ($cor == "#FFFFFF") ? "#B7E8FF" : "#FFFFFF";
			$strResp .= '<tr style="background-color:'.$cor.'" >
							<td>'.utf8_encode($ln["desc_cliente"]).'</td>
							<td>'.$ln["data_prev_servico"].'</td>
							<td>'.utf8_encode($ln["assunto_servico"]).'</td>
							<td>'.number_format($ln["vlr_servico"],2,',','.').'</td>
							<td>'.number_format($ln["vlr_desc_servico"],2,',','.').'</td>
							<td>'.utf8_encode($ln["desc_status"]).'</td>
							<td>'.utf8_encode($ln["desc_tipo_pagamento"]).'</td>
							<td>'.utf8_encode($ln["desc_usuario"]).'</td>
						</tr>';	
		}		
	}else{
		$strResp .= '<tr><td colspan="8">Nenhum registro encontrato</td></tr>';
	}
	
	$strResp .= '	</tbody>
				</table>'; 
	
	unset($_SESSION["relatorio"]);
	$_SESSION["relatorio"] = $strResp;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta name="description" content="" />
        <meta name="keywords" content="" />
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>SISAT "Sistema de Atendimento Externo"</title>
        <!-- Chamada da página de estilo da Página -->
        <link rel="stylesheet" type="text/css" href="../includes/css/estilo.css" />
        <!-- Chamada da página de estilo da Página do Login -->
        <link rel="stylesheet" type="text/css" href="../includes/css/login.css" />
        <link rel="stylesheet" type="text/css" href="../includes/css/fullcalendar.css" />
        <link rel="stylesheet" type="text/css" href="../includes/css/fullcalendar.print.css" />
        <link rel="stylesheet" type="text/css" href="../includes/css/demos.css" />
        <link rel="stylesheet" type="text/css" href="../includes/css/cadastro.css" />
        <link rel="stylesheet" type="text/css" href="../includes/css/tabs.css" />
		<link rel="stylesheet" type="text/css" href="../includes/css/jquery.ui.theme.css" />

        <script type="text/javascript" src="../includes/js/jquery.js"></script>
        <script type="text/javascript" src="../includes/js/jquery-ui-1.8.17.custom.min.js"></script>
        <script type="text/javascript" src="../includes/js/fullcalendar.js"></script>
        <script type="text/javascript" src="../includes/js/gcal.js"></script>
        <script type="text/javascript" src="../includes/js/jquery.bgiframe-2.1.2.js"></script>
        <script type="text/javascript" src="../includes/js/jquery-ui.min.js"></script>
        <script type="text/javascript" src="../includes/js/funcoes.js"></script>
        <script type="text/javascript" src="../includes/js/jquery.ui.core.js"></script>
        <script type="text/javascript" src="../includes/js/jquery.ui.widget.js"></script>
        <script type="text/javascript" src="../includes/js/jquery.ui.tabs.js"></script>
        <script type="text/javascript" src="../includes/js/jquery.maskedinput-1.1.4.pack.js"></script>
        <script>
            $(document).ready(function() {
            	$("#btBuscar").click(function(){
                	cont = false;
					if($("#strCliente").val() != ""){
						cont = true;
					}
					if($("#idStatusPagamento").val() != ""){
						cont = true;
					}
					if($("#idTipoPagamento").val() != ""){
						cont = true;
					}
					if($("#idTecnico").val() != ""){
						cont = true;
					}
					if($("#flPagamento").is(":checked")){
						cont = true;
					}

					if(cont){
						$("#formRel").submit();
					}else{
						alert("Informe pelo menos um parâmentro!");
					}
                });
            });

        </script>
    </head>
    <body>
		<div id="wrapper">
			<div id="header">
				<div id="logo">
					<h1></h1>
				</div>
				<div id="menu">
					<?php include '../includes/menu.php'?>
				</div>
			</div>
			<div id="pcadastro">
				<form id="formRel" method="post" action="">
					<h1 style="font-size:2em;">Relatório</h1>
					<div id="content" style="width:955px;">
						<table width="100%" border="0">
							<tr>
								<td style="width:120px;">Cliente:</td>
								<td>
									<input type="text" name="strCliente" id="strCliente" value="<?php echo $_POST["strCliente"]; ?>"/>
								</td>
							</tr>
							<tr>
								<td>Status:</td>
								<td>
									<select name="idStatusPagamento" id="idStatusPagamento">
										<option value=""></option>
										<?php include 'statusAtendimento.php'; ?>
									</select>
								</td>
							</tr>
							<tr>
								<td>Forma de pagamento:</td>
								<td>
									<select name="idTipoPagamento" id="idTipoPagamento">
										<option value=""></option>
										<?php include 'tipoPagamento.php'; ?>
									</select>
								</td>
							</tr>
							<tr>
								<td>Técnico:</td>
								<td>
									<select name="idTecnico" id="idTecnico">
										<option value=""></option>
										<?php include 'tecnicos.php'; ?>
									</select>
								</td>
							</tr>
							<tr>
								<td>Pagamente realizado:</td>
								<td>
									<input type="checkbox" name="flPagamento" id="flPagamento" value="1" <?php echo ($_POST["flPagamento"] == 1) ? "checked" : ""; ?>/>
								</td>
							</tr>
							<tr>
								<td colspan="2">
									<input type="button" name="btVoltar" id="btVoltar" value="Voltar" class="btn"/>
									<input type="button" name="btBuscar" id="btBuscar" value="Buscar" class="btn"/>
								</td>
							</tr>
						</table>
						<br/><br/>
						<?php 
							if(!empty($strResp)) {echo '<input type="button" name="btImprimir" id="btImprimir" value="Imprimir" class="btn" onclick="imprimeRel();"/>'; }
							echo $strResp; 
						?>
					</div>
				</form>
			</div>
		</div>
	<br class="clearfix" />
		<div id="footer">Copyright (c) 2012 SISAT "Sistema de Atendimento Externo."</div>
    </body>
</html>
